﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using Evoucher.Business;
using System.Configuration;
using Evoucher.Business.Models;
using Mani.Common;
using System.Web.Security;

public partial class _Default : System.Web.UI.Page
{
    const int IRANCELL = 1;
    const int TALIYA = 2;
    const int HAMRAH = 3;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            throw new Exception("Fuck You!");
            GetIranCellVoucherTypes();
            GetTaliyaVoucherTypes();
            GetHamrahAvalVoucherTypes();
            GetActivePaymentGateway();
            GenerateCaptcha();
        }
    }

    private void GetActivePaymentGateway()
    {
        IVoucherRepository repo = new VoucherRepository();
        List<PaymentGateway> pay = repo.GetPaymentGateways();
        if (pay.Count == 0)
        {
            DDSelectBank.Visible = false;
            return;
        }

        DDSelectBank.DataSource = pay.Where(p => p.Active.Value == true);
        DDSelectBank.DataValueField = "Id";
        DDSelectBank.DataTextField = "Name";
        DDSelectBank.DataBind();
    }

    private void GenerateCaptcha()
    {
        Session["CaptchaImageText"] = GenerateRandomCode();
    }

    private string GenerateRandomCode()
    {
        string s = "";
        Random rand = new Random();
        for (int i = 0; i <= 5; i++)
        {
            s = s + rand.Next(0, 10).ToString();
        }
        return s;
    }

    private void GetTaliyaVoucherTypes()
    {
        IVoucherRepository repo = new VoucherRepository();
        List<VoucherType> voucherTypes = repo.GetVoucherTypesByProviderId(TALIYA).Where(vt => vt.Active).ToList<VoucherType>();
        if (voucherTypes.Count == 0)
        {
            taliyaUnderConst.Visible = true;
            return;
        }

        rbTaliya.DataSource = voucherTypes;
        rbTaliya.DataValueField = "Id";
        rbTaliya.DataTextField = "Description";
        rbTaliya.DataBind();
        taliyaUnderConst.Visible = false;
    }

    private void GetHamrahAvalVoucherTypes()
    {
        IVoucherRepository repo = new VoucherRepository();
        List<VoucherType> voucherTypes = repo.GetVoucherTypesByProviderId(HAMRAH).Where(vt => vt.Active).ToList<VoucherType>();
        if (voucherTypes.Count == 0)
        {
            hamraheAvalUnderConst.Visible = true;
            return;
        }

        rbHamrah.DataSource = voucherTypes;
        rbHamrah.DataValueField = "Id";
        rbHamrah.DataTextField = "Description";
        rbHamrah.DataBind();
        hamraheAvalUnderConst.Visible = false;
    }

    private void GetIranCellVoucherTypes()
    {
        IVoucherRepository repo = new VoucherRepository();
        List<VoucherType> voucherTypes = repo.GetVoucherTypesByProviderId(IRANCELL).Where(vt => vt.Active).ToList<VoucherType>();
        if (voucherTypes.Count == 0)
        {
            iranCellUnderConst.Visible = true;
            return;
        }

        rbIranCell.DataSource = voucherTypes;
        rbIranCell.DataValueField = "Id";
        rbIranCell.DataTextField = "Description";
        rbIranCell.DataBind();
        iranCellUnderConst.Visible = false;
    }

    protected void btnPayment_Click(object sender, EventArgs e)
    {
        if (Session["CaptchaImageText"] == null || Session["CaptchaImageText"].ToString() != tbNumImage.Text)
        {
            UIHelper.ClientAlert(this, "لطفا عدد در تصویر را به دقت وارد کنید");
            GenerateCaptcha();
            return;
        }

        if (String.IsNullOrEmpty(tbEmail.Text) || String.IsNullOrEmpty(tbMobile.Text))
        {
            UIHelper.ClientAlert(this, "وارد کردن اطلاعات شماره تلفن همراه و آدرس پست الکترونیکی الزامی است");
            return;
        }

        if (String.IsNullOrEmpty(rbIranCell.SelectedValue) && String.IsNullOrEmpty(rbTaliya.SelectedValue) && String.IsNullOrEmpty(rbHamrah.SelectedValue))
        {
            UIHelper.ClientAlert(this, "لطفا نوع کارت شارژ مورد نظر خود را انتخاب کنید");
            return;
        }


        IVoucherRepository repo = new VoucherRepository();

        int GatewayId = Convert.ToInt32(DDSelectBank.SelectedValue);
        PaymentGateway pg = repo.GetGatewayInfo(GatewayId);
        int count = Convert.ToInt32(VoucherCount.Value);

        aspnet_Users u = null;
        MembershipUser user = Membership.GetUser();
        if (user != null)
            u = repo.GetUserByName(user.UserName);

        int selectedVoucherType = 0;

        if (!String.IsNullOrEmpty(rbIranCell.SelectedValue))
            selectedVoucherType = Convert.ToInt32(rbIranCell.SelectedValue);
        else if (!String.IsNullOrEmpty(rbTaliya.SelectedValue))
            selectedVoucherType = Convert.ToInt32(rbTaliya.SelectedValue);
        else if (!String.IsNullOrEmpty(rbHamrah.SelectedValue))
            selectedVoucherType = Convert.ToInt32(rbHamrah.SelectedValue);

        Transaction t = repo.InitTransaction(u, tbEmail.Text, tbMobile.Text, pg, count, selectedVoucherType);

        if (t == null)
        {
            UIHelper.ClientAlert(this, "کاربر گرامی با عرض پوزش شارژ کارت انتخابی شما در حال حاضر موجود نمی باشد");
            return;
        }

        repo.persist();

        Session["transaction"] = t;

        RemotePost post = new RemotePost(pg.GatewayUrl);
        post.Add("Amount", t.Amount.ToString());
        post.Add("ResNum", t.Id.ToString());
        post.Add("MID", pg.MerchantId);
        post.Add("RedirectURL", ConfigurationManager.AppSettings["RedirectURL"]);

        post.Post(this);
    }
}